package cn.support.neo.mvp.core.provider;

import android.content.Context;
import android.util.Log;

import com.github.pwittchen.prefser.library.Prefser;

import java.io.File;
import java.util.concurrent.TimeUnit;

import cn.support.neo.mvp.BuildConfig;
import cn.support.neo.mvp.core.base.Constants;
import cn.support.neo.mvp.core.service.intercaptor.CacheInterceptor;
import cn.support.neo.mvp.core.service.intercaptor.OAuthInterceptor;
import cn.support.neo.mvp.core.service.intercaptor.ProgressInterceptor;
import cn.support.neo.mvp.core.service.progress.ProgressListener;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Name:OKhttp工具
 * Author: WK（wangkun@xjzhicheng.com)
 * Date: 2016-06-21
 * Describe: PLEASE TO FIXME...
 */
public class OkHttpClientProvider {

    private static OkHttpClient okHttpClient;
    private static OkHttpClient okHttpClient4Progress;
    private static Cache cache;

    private OkHttpClientProvider() {
    }

    public static OkHttpClient getInstance(Context context, final Prefser prefser, final TokenProvider tokenProvider) {
        if (okHttpClient == null) {
            okHttpClient = OkHttpClientBuilder(context, tokenProvider).build();
        }
        return okHttpClient;
    }

    public static OkHttpClient getProgressInstance(Context context,
                                                   final Prefser prefser,
                                                   final TokenProvider tokenProvider,
                                                   ProgressListener progressListener) {
        if (okHttpClient4Progress == null) {
            OkHttpClient.Builder builder = OkHttpClientBuilder(context, tokenProvider);
            builder.addInterceptor(new ProgressInterceptor(progressListener));
            okHttpClient4Progress = builder.build();
        }
        return okHttpClient4Progress;
    }

    private static OkHttpClient.Builder OkHttpClientBuilder(Context context, TokenProvider tokenProvider) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //Http日志
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addNetworkInterceptor(logging);
        }
        //Http读取超时
        builder.readTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.SECONDS);//读取超时
        //Http连接超时
        builder.connectTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.SECONDS).build();//连接超时
        //Http缓存
        try {
            File cacheDir = new File(context.getCacheDir(), Constants.RESPONSE_CACHE);
            if (cache == null) {
                //40M缓存
                cache = new Cache(cacheDir, 40 * 1024 * 1024);
            }
        } catch (Exception e) {
            Log.e("OKHttp", "Could not create http cache !", e);
        }
        builder.cache(cache);
        builder.addInterceptor(new CacheInterceptor(context));
        builder.addInterceptor(new OAuthInterceptor(tokenProvider));
        //Http-Header拦截器
        builder.cookieJar(new NativeCookieManger(context));
        return builder;
    }

}
